home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5004 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.3 KB  |  60 lines

  1. Path: newsstand.cit.cornell.edu!ub!newserve!rebecca!rpi!not-for-mail
  2. From: clamage@Eng.Sun.COM (Steve Clamage)
  3. Newsgroups: comp.lang.c++,comp.lang.c++.moderated
  4. Subject: Re: Defining Classes and Class Functions extern: 2 Line Sample
  5. Date: 1 Feb 1996 17:06:12 -0000
  6. Organization: Sun Microsystems Inc., Mountain View, CA
  7. Sender: cppmods@netlab.cs.rpi.edu
  8. Approved: devitto@ferndown.ate.slb.com
  9. Message-ID: <4eqru4$bhg@netlab.cs.rpi.edu>
  10. References: <4em2dn$kio@netlab.cs.rpi.edu>
  11. NNTP-Posting-Host: netlab.cs.rpi.edu
  12. X-Original-Date: 1 Feb 1996 06:35:43 GMT
  13.  
  14. ivo.welch@AGSM.UCLA.EDU (Ivo Welch) writes:
  15.  
  16. >Why does the following not work?
  17. >    class tryclass;
  18. >    extern int tryclass::tryclassfun(int w);
  19.  
  20. Because you are not allowed to mention a class member unless the
  21. class definition is visible. For one thing, allowing this sort
  22. of thing would allow you in principle to insert members into a
  23. class without modifying the class. More importantly, the compiler
  24. might not have the information it needs to deal with a call to
  25. the member function without seeing the whole class definition.
  26.  
  27. Some languages allow you to do all of these things, but C++ does not.
  28.  
  29. >Is there an equivalent recommended way of hiding many member 
  30. >functions/declarations from a particular file?
  31.  
  32. A common technique is to put only a pointer to an undefined
  33. implementation class in the main class header file. Only the
  34. implementation of the functions for that class ever see
  35. the hidden class.
  36.  
  37.     class FooImpl; // forward declaration, FooImpl does all the work
  38.  
  39.     class Foo { // this is what users see
  40.     private:
  41.         FooImpl* impl;
  42.     public:
  43.         ... all the member functions of Foo call FooImpl functions
  44.     };
  45.  
  46. This technique, described in many C++ books, can be used to hide
  47. all or some of the implementation details of Foo from users of
  48. Foo. In particular, it means that implementation details can
  49. be changed without requiring that clients of Foo be recompiled.
  50. That is, you can add, remove, or reorder any and all of the
  51. members of FooImpl at will. Only the member functions of Foo
  52. need to be recompiled.
  53. --
  54. Steve Clamage, stephen.clamage@eng.sun.com
  55.  
  56.       [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
  57.       [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
  58.       [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
  59.       [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
  60.